package com.szrt.java.book.client;


import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;

import com.szrt.java.book.entity.BookEntity;
import com.szrt.java.book.service.BookService;
import com.szrt.java.book.utils.BookUtil;

public class BookClient {
	
	/**
	 * 删除一个节点 倚天屠龙记
	 * @throws Exception
	 * http://xurichusheng.iteye.com/blog/2065359
	 */
	@Test
	public void delete() throws Exception {
		//SAXReader reader = new SAXReader();
		//Document doc = reader.read(new File("src/main/java/szrt.xml")); 
		
		
		
		InputStream is = null;  
        OutputStream out = null;  
        XMLWriter xmlwriter = null;  
        Document doc = null;
  
        try {  
            SAXReader sax = new SAXReader();  
  
            is = Thread.currentThread().getContextClassLoader().getResourceAsStream("src/main/java/szrt.xml");  
  
            doc = sax.read(is);  
  
            // 根节点  
            Element root = doc.getRootElement();  
  
            // 取得某节点下名为"menu"的所有字节点  
            List nodes = root.elements("menu");  
  
            // xml元素  
            Element element = null;  
            Map<String, String> map = new ConcurrentHashMap<String, String>();  
  
            for (Object obj : nodes) {  
                element = (Element) obj;  
                // 菜单编号  
                String menuNo = null;  
                // 删除指定节点下的子节点的值  
                if ("1005".equals(element.attributeValue("id"))) {  
  
                    menuNo = element.element("book").getTextTrim();  
  
                    map.put("id", menuNo);  
                    // 删除nodeId对应的菜单节点  
                    element.detach();  
                }  
  
                // 节点编号  
                menuNo = element.element("id").getTextTrim();  
  
                if (null != map.get("p_menuNo")  
                        && !"".equals(map.get("p_menuNo"))) {  
  
                    if (null != menuNo  
                            && menuNo.startsWith(map.get("p_menuNo")  
                                    .concat("-"))) {  
                        /* menuNo是 p_menuNo 的子节点 */  
                        // 删除子节点  
                        element.detach();  
                    }  
                }  
            }  
  
            // 输出格式  
            OutputFormat outformat = new OutputFormat();  
            // 指定XML编码  
            outformat.setEncoding("UTF-8");  
            outformat.setNewlines(true);  
            outformat.setIndent(true);  
            outformat.setTrimText(true);  
  
            String rootPath = System.getProperty("user.dir");  
  
            out = new FileOutputStream(rootPath + "src/main/java/szrt.xml");  
  
            xmlwriter = new XMLWriter(out, outformat);  
  
            xmlwriter.write(doc);  
  
        } catch (DocumentException e) {  
            //log.error("Modify Node fail:" + e.getMessage(), e);  
            throw e;  
        } catch (Exception e) {  
            //log.error("Modify Node fail:" + e.getMessage(), e);  
            throw e;  
        } finally {  
            //close(xmlwriter, out, is);  
        }  
		
	}
	
	
	
	/**
	 * 打印xml文档到控制台
	 * @throws Exception
	 */
	@Test
	public void print() throws Exception {
		BookService service = new BookService();
		List<BookEntity> books = service.getBooks();
		
		//开始构建1个xml文档
		Document document = DocumentHelper.createDocument();
		
		//创建文档的根节点
		Element root = document.addElement("books");
		
		for(BookEntity book : books){
			service.insertNode(book, root);
		}
		
		BookUtil.print(document);
	}
	
	/**
	 * 打印xml到指定文件中
	 * @throws Exception
	 */
	@Test
	public void add() throws Exception {
		
		BookService service = new BookService();
		
		List<BookEntity> books = service.getBooks();
		
		
		//开始构建1个xml文档
		Document document = DocumentHelper.createDocument();
		
		//创建文档的根节点
		Element root = document.addElement("books");
		
		for(BookEntity book : books){
			service.insertNode(book, root);
		}
		//String file ="c:/szrt.xml";
		String file ="src/main/java/szrt.xml";
		BookUtil.add(document, file);
	}

}
